package cn.edu.lzu.service.impl;

import cn.edu.lzu.common.assist.DataPage;
import cn.edu.lzu.common.assist.ResponseResult;
import cn.edu.lzu.dao.RadiologyExamineItemMapper;
import cn.edu.lzu.entity.RadiologyExamineItem;
import cn.edu.lzu.service.RadiologyExamineItemService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.xml.crypto.Data;
import java.util.List;

/**
 * @PackageName: cn.edu.lzu.service.impl
 * @ClassName: RadiologyExamineItemServiceImpl
 * @Description: TODO
 * @Author: 张琦[QQ:3098086691]
 * @Date: 2020/3/23 21:02
 * @Version: 1.0
 */
@Service
@Transactional
public class RadiologyExamineItemServiceImpl implements RadiologyExamineItemService {
    @Resource
    private RadiologyExamineItemMapper radiologyExamineItemMapper;

    @Override
    public DataPage getRadiologyExamineItemList(long unitId, int currentPageNum, int pageSize) {
        int startIndex=(currentPageNum-1)*pageSize;
        List<RadiologyExamineItem> radiologyExamineItemList=radiologyExamineItemMapper.selRadItemsByUnitId(unitId, startIndex, pageSize);
        int totalCount=radiologyExamineItemMapper.selTotalCountRadItemsByUnitId(unitId);
        DataPage dataPage=new DataPage();
        dataPage.setData(radiologyExamineItemList);
        dataPage.setTotalCount(totalCount);
        dataPage.setCode(0);
        dataPage.setMsg("成功");
        return dataPage;
    }

    /**
     * @Author: 张琦[QQ:3098086691]
     * @Description: TODO 添加radiologyExamineItem一条记录
     * @Date: 2020/3/23 22:52
     * @Param: [radiologyExamineItem]
     * @Return: cn.edu.lzu.common.assist.ResponseResult
     **/
    @Override
    public ResponseResult addRadExamineItem(RadiologyExamineItem radiologyExamineItem) {
        int result=radiologyExamineItemMapper.insRadItem(radiologyExamineItem);
        ResponseResult responseResult=new ResponseResult();
        if(result==1){
            responseResult.setCode(1);
            responseResult.setMsg("添加成功！");
        }else{
            responseResult.setCode(0);
            responseResult.setMsg("添加失败！");
        }
        return responseResult;
    }

    /**
     * @Author: 张琦[QQ:3098086691]
     * @Description: TODO 根据examineItemId删除radiologyExamineItem
     * @Date: 2020/3/24 20:27
     * @Param: [examineItemId]
     * @Return: cn.edu.lzu.common.assist.ResponseResult
     **/
    @Override
    public ResponseResult deleteRadItemById(int examineItemId) {
        int result=radiologyExamineItemMapper.updDeleteRadItemById(examineItemId);
        ResponseResult responseResult = new ResponseResult();
        if (result == 1) {
            responseResult.setCode(1);
            responseResult.setMsg("删除成功！");
        } else {
            responseResult.setCode(0);
            responseResult.setMsg("删除失败");
        }
        return responseResult;
    }

    @Override
    public DataPage getFuzzySearchRadItem(int searchType, String searchInput, long unitId, int currentPageNum, int pageSize) {
        DataPage dataPage = new DataPage();
        List<RadiologyExamineItem> radiologyExamineItemList=null;
        //如果输入不为空，执行模糊搜索
        if (searchInput != null && searchInput!= "") {
            //根据clauseName搜索
            if (searchType == 1) {
                radiologyExamineItemList=radiologyExamineItemMapper.selFuzzyRadItemByName(unitId, searchInput);

            } else if (searchType == 2) {
                //根据codeName搜索
                radiologyExamineItemList=radiologyExamineItemMapper.selFuzzyRadItemByArea(unitId, searchInput);

            } else if (searchType == 3) {
                //根据所属项目名称搜索
                radiologyExamineItemList=radiologyExamineItemMapper.selFuzzyRadItemByMethod(unitId, searchInput);
            }
            dataPage.setData(radiologyExamineItemList);
            if(radiologyExamineItemList!=null){
                dataPage.setMsg("数据获取成功！");
                dataPage.setCode(0);
            }else{
                dataPage.setMsg("数据获取失败！");
                dataPage.setCode(1);
            }
            dataPage.setTotalCount(0);
            return dataPage;
        } else {
            //执行顺序搜索，根据currentPageNum和pageSize获取一页数据
            System.out.println("输入为空");
            dataPage=getRadiologyExamineItemList(unitId, currentPageNum,  pageSize);
            System.out.println(pageSize+"\t"+currentPageNum);
            System.out.println(dataPage);
            return dataPage;
        }
    }

    @Override
    public List<RadiologyExamineItem> getAllRadItems(long unitId) {
        List<RadiologyExamineItem> radiologyExamineItemList=radiologyExamineItemMapper.selAllRadItems(unitId);
        return radiologyExamineItemList;
    }
}
